package com.liner.inner;

import java.util.Arrays;

public class BubbleSort {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 43, 45, 6, 27, 12, 9, 7, 2, 16};
        //sort1(numbers);
        sort(numbers);
        System.out.println(Arrays.toString(numbers));

    }

    private static void sort(int[] numbers) {
        for (int i = 0; i < numbers.length - 1; i++) {
            boolean flag = false;
            for (int j = 0; j < numbers.length - 1 - i; j++) {
                if (numbers[j] > numbers[j + 1]) {
                    int temp = numbers[j];
                    numbers[j] = numbers[j + 1];
                    numbers[j + 1] = temp;
                    flag = true;
                }

            }
            if (!flag) {
                break;
            }
        }

    }

    private static void sort1(int[] numbers) {
        for (int i = 0; i < numbers.length - 1; i++) {
            //内层循环控制到达位置
            for (int j = 0; j < numbers.length - 1 - i; j++) {
                //比较前面的元素比后面元素大交换，同理也可以比较小
                if (numbers[j] > numbers[j + 1]) {
                    int temp = numbers[j];
                    numbers[j] = numbers[j + 1];
                    numbers[j + 1] = temp;
                }
            }
        }
    }

}